package com.dpp.controller.user;

import com.dpp.constant.JwtClaimsConstant;
import com.dpp.context.BaseContext;
import com.dpp.entity.*;
import com.dpp.mapper.ActivityMapper;
import com.dpp.mapper.ActivityRecordMapper;
import com.dpp.mapper.OrderMapper;
import com.dpp.properties.JwtProperties;
import com.dpp.result.Result;
import com.dpp.service.ActivityService;
import com.dpp.service.CouponBookService;
import com.dpp.service.OrderService;
import com.dpp.service.UserService;
import com.dpp.utils.JwtUtil;
import com.dpp.vo.HomeLists;
import com.dpp.vo.HomeVO;
import com.dpp.vo.RabbitQR;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user/home")
public class HomeController {


    @Autowired
    private OrderService orderService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private ActivityRecordMapper activityRecordMapper;
    @Autowired
    private UserService userService;
    @GetMapping("/JTQR")
    public void JTQR(String code, HttpServletResponse resp) throws IOException {
        Users user = userService.login(code);
        if (user != null) {
            HashMap<String, Object> claimsMap = new HashMap<>();
            claimsMap.put(JwtClaimsConstant.OPEN_ID, user.getOpenid());
            //创建jwt令牌
            String Newtoken = JwtUtil.createJWT(
                    jwtProperties.getUserSecretKey(),
                    jwtProperties.getUserTtl(),
                    claimsMap);
            //将jwt令牌给前端，内容是用户的openid
            String url = jwtProperties.getUrl().concat("/components/Utils/barCode.html?token=" + Newtoken);
            resp.sendRedirect(url);
        }
    }

    @GetMapping("/rabbitQR")
    public Result rabbitQR() throws IOException {
        String openId = BaseContext.getCurrentId();
        BufferedImage image=  userService.showQR(openId);
        if(image==null){
            return Result.error("请填写取件信息");
        }
        ByteArrayOutputStream outStream =new ByteArrayOutputStream();
        ImageIO.write(image, "JPEG", outStream);
        //把outStream里的数据写入内存
        RabbitQR rabbitQR = new RabbitQR();
        rabbitQR.setRabbitQR(outStream.toByteArray());
        return Result.success(rabbitQR);
    }

    /**
     * 查询所有订单
     * @return
     */
    @GetMapping("/getAll")
    public Result<HomeLists> getAll(){
        HomeLists homeLists = orderService.selectAll();
        return Result.success(homeLists);
    }
    @GetMapping("/checkNew")
    public Result checkNew(){
        String openId = BaseContext.getCurrentId();
        List<ActivityRecord> activityRecords = activityRecordMapper.selectByOpenid(openId);
        for (ActivityRecord activityRecord : activityRecords) {
            Integer activityId = activityRecord.getActivityId();
            if(activityId.equals(1)){
                return Result.error("活动已参加");
            }
        }
        return Result.success();
    }

    @GetMapping("/advert")
    public Result getAdvert(){
        String openId = BaseContext.getCurrentId();
        return activityService.getAdvert(openId);
    }
    @GetMapping("/carousel")
    public Result getCarousel(){
        //根据Activity_record的记录，来跳转不同的url
        //0.1元和0.33元活动，Activity_record没有记录，则下单插入；确认结束
        List<Carousel> carouselList=new ArrayList<>();
        String openId = BaseContext.getCurrentId();
        //0、0.1下单纸巾的活动，点击跳转到下单页
        //todo

        //1、点击跳转新人0.33单的页面，参加过了提示不能参与
        //Carousel carousel1= Carousel.builder()
        //        .imgUrl(jwtProperties.getUrl().concat("/img/").concat("ad1.jpg"))
        //        .adlink(jwtProperties.getUrl().concat("/components/Activities/salesPromotion.html"))
        //        .popping(0)
        //        .poppingImg("").build();
        //
        //List<ActivityRecord> activityRecords = activityRecordMapper.selectByOpenid(openId);
        //for (ActivityRecord activityRecord : activityRecords) {
        //    Integer activityId = activityRecord.getActivityId();
        //    if(activityId.equals(1)){
        //        carousel1.setPopping(2);
        //        carousel1.setMsg("活动已参加");
        //    }
        //
        //}
        //下面这3个都是弹出客服二维码的窗口
        //4、人人为我：点击弹出添加客服的二维码
        Carousel carousel3= Carousel.builder()
                .imgUrl(jwtProperties.getUrl().concat("/img/").concat("ad5.png"))
                .adlink("")
                .popping(1)
                .poppingImg(jwtProperties.getUrl().concat("/img/").concat("HelperQR.png")).build();
        //2、加入我们：点击弹出添加客服的二维码
        Carousel carousel4= Carousel.builder()
                .imgUrl(jwtProperties.getUrl().concat("/img/").concat("ad2.jpg"))
                .adlink("")
                .popping(1)
                .poppingImg(jwtProperties.getUrl().concat("/img/").concat("HelperQR.png")).build();

        //3、更多板块：点击弹出尽请期待的对话框，提示建议联系客服
        Carousel carousel5= Carousel.builder()
                .imgUrl(jwtProperties.getUrl().concat("/img/").concat("ad3.png"))
                .adlink("")
                .popping(1)
                .poppingImg(jwtProperties.getUrl().concat("/img/").concat("Know.png")).build();

//        Carousel carousel6= Carousel.builder()
//                .imgUrl(jwtProperties.getUrl().concat("/img/").concat("Invite.png"))
//                .adlink(jwtProperties.getUrl().concat("/components/Activities/codePromotion.html"))
//                .popping(0)
//                .poppingImg("").build();
//        carouselList.add(carousel1);
//        carouselList.add(carousel6);
        carouselList.add(carousel3);
        carouselList.add(carousel4);
        carouselList.add(carousel5);
        return Result.success(carouselList);
    }
}
